home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / alib10.zip / LOAN.ASM < prev    next >
Assembly Source File  |  1994-02-25  |  10KB  |  434 lines

  1. ;*****************************  LOAN.ASM   **********************************
  2. PAGE  70,132
  3. comment 
  4.                              LOAN Payments
  5.                              -------------
  6.  
  7.      Purpose:
  8.      --------
  9.  
  10.      LOAN is a sample program to show how floating point math can
  11.      be performed.  It will calculate a 12 month table showing
  12.      what portions of payments go to principal and interest.
  13.  
  14.      Using LOAN
  15.      ----------
  16.  
  17.      To calculate the loan parameters, type "LOAN"<Enter> and
  18.      enter the loan amount, principal, and interest.  The
  19.      program will do the rest.
  20.  
  21.      Floating Point Overview
  22.      -----------------------
  23.  
  24.      LOAN operates by first allocating 8 floating variables.  Then,
  25.      three of the variables are set to the payment amount, principal,
  26.      and interest respectivly.  Each of the 8 allocated variables
  27.      are referenced by a number or token.  The first variable is #1,
  28.      the second #2 etc.  This makes it vary easy to add two variables
  29.      by just saying add #1 to #2.  When, the calculations are done,
  30.      they are extracted by calling "text_out" to convert from 
  31.      floating point format to a printable ascii text string.
  32.  
  33.      Compiling
  34.      ---------
  35.  
  36.      The commands needed to build LOAN.EXE using MASM are:
  37.         masm loan;
  38.         link loan,loan,,alib.lib;
  39. 
  40.      
  41.     include    mac.inc
  42.     include    common.inc
  43. ;-----------------------------------------------------------------------------
  44.     extrn    library_setup:far
  45.     extrn    lib_error_handler:far
  46.     extrn    clear_screen:far
  47.     extrn    display_string:far
  48.     extrn    library_terminate:far    
  49.     extrn    qget_string:far
  50.     extrn    key_or_mouse:far
  51.     extrn    binary_in:far
  52.     extrn    binary_out:far
  53.     extrn    f_divide:far
  54.     extrn    f_multiply:far
  55.     extrn    f_subtract:far
  56.     extrn    f_addition:far
  57.     extrn    text_in:far
  58.     extrn    text_out:far
  59.     extrn    fmove:far
  60.     extrn    round:far    
  61. ;------------------------------------------------------------------------------
  62. code        segment para public 'CODE'
  63.         assume    cs:code, ds:code
  64. ;-----------------------------------------------------------------------------
  65. ;
  66. color        db    1bh
  67.  
  68. pspseg        dw    0
  69. lib_info_ptr    dw    0
  70. lib_info_seg    dw    0
  71.  
  72. copyright_msg    db    '----------------- LOAN PAYMENT table ------------- ',0
  73.  
  74. interest_msg    db    'What is the interest rate (xx.xx)% ',0
  75. interest_buf    db    8 dup (0)
  76. amount_msg    db    'What is principle remaining ',0
  77. amount_buf    db    20 dup (0)
  78. payment_msg    db    'What is amount of payments ',0
  79. payment_buf    db    12 dup (0)
  80. header_msg    db    'PAYMENT    INT-PAID    PRIN-PAID   PRIN-REMAINING',0
  81. term_dash_msg    db    '-------    --------    ----------- --------------',0
  82. any_key_msg    db    'Press any key to continue',0
  83.  
  84. int_paid_text    db    15 dup (0)
  85. prin_paid_text    db    15 dup (0)
  86. prin_remain_text db    15 dup (0)
  87.  
  88. total_prin_paid    db    15 dup (0)
  89. total_int_paid    db    15 dup (0)
  90.  
  91. display_row    db    8
  92. month_count    db    0
  93.  
  94.         dw    200 dup (0)        ;stack
  95. stack_        dw    0
  96. ;-----------------------------------------------------------------------------
  97. start:
  98.     cli
  99.     mov    cs:pspseg,es    ;save PSP segment
  100.     mov    ax,cs        ;get CODE segment
  101.     mov    ss,ax
  102.     mov    ds,ax
  103.     mov    es,ax
  104.     mov    sp,offset stack_
  105.     sti
  106.     
  107. ; next, release memory beyond the end of the program
  108. ; The  definition for ZSEG marks the
  109. ; end of the program's code, data and stack area.
  110. ; When linking be sure ZSEG is at the end of the program.
  111.  
  112.     mov    ax,zseg
  113.  
  114.     mov    bx,cs:pspseg        ;
  115.     mov    es,bx
  116.     sub    bx,ax
  117.     neg    bx            ; size of program in paragraphs
  118.     mov    ah,4Ah            ; resize memory block
  119.     int    21h
  120.  
  121.     mov    ax,cs
  122.     mov    es,ax
  123. ;
  124. ; check if enough memory free to run program
  125. ;
  126.     mov    ax,pspseg        ;pass psp segment to setup
  127.     mov    bx,8            ;number of floating point variables
  128.     call    library_setup
  129.     mov    lib_info_ptr,si        ;save ptr to library info block
  130.     mov    lib_info_seg,es         ; see COMMON.INC or LIBRARY_SETUP
  131.     cmp    ax,128
  132.     jae    got_enough_mem        ;jmp if 128k of memory avail
  133.     mov    al,7
  134.     mov    ah,fatal_return
  135.     call    lib_error_handler
  136.     jmp    exitx
  137.     
  138. got_enough_mem:
  139. ;
  140. ; clear the screen and display signon
  141. ;
  142.     mov    ah,color        ;select color
  143.     mov    al,' '
  144.     call    clear_screen
  145. ;
  146. ; display copyright message
  147. ;
  148.     mov    si,offset copyright_msg
  149.     mov    ah,color
  150.     mov    dx,0100h        ;display row
  151.     call    display_string    
  152.     
  153.     
  154. ; prevent an unintended program crash; trap Ctrl+Break, Ctrl+C and
  155. ; Ctrl+Alt+Del key combinations
  156.  
  157. ;    call    BREAK_KEY_INTERCEPT
  158. ;
  159. ; --- --- --- --- ---
  160. ; load floating point variables as follows: token 0 = interest rate
  161. ;                                                 1 = prin
  162. ;                                                 2 = payment
  163. ;                                                 3 = interest paid
  164. ;                                                 4 = prin paid
  165. ;                                                 5 = temp (x)
  166. ;                                                 6 = total int. paid
  167. ;                                                 7 = total prin paid
  168. ; get varialbes & zero rest
  169. ; int = int/1200
  170. ; int paid = int * prin
  171. ; prin paid = payment - int paid
  172. ; x = prin - prin paid
  173. ; if x is positive
  174. ;   prin = x
  175. ;   total int = total int + int paid
  176. ;   total prin= total prin + prin paid
  177. ;
  178. ;
  179. ; get beginning loan amount (prin)
  180. ;
  181.     mov    ax,cs
  182.     mov    ds,ax
  183.     mov    es,ax
  184.     
  185.     mov    si,offset amount_msg
  186.     mov    dx,0201h            ;display location
  187.     mov    ah,color
  188.     call    display_string
  189.     mov    cl,10                ;max length of input string
  190.     mov    dx,0222h            ;display location
  191.     mov    di,offset amount_buf
  192.     call    qget_string
  193.     mov    ax,1                ;token #
  194.     call    text_in                ;es:di point at text, ax=token
  195. ;
  196. ; get payment amount
  197. ;
  198.     mov    si,offset payment_msg
  199.     mov    dx,0301h            ;display location
  200.     mov    ah,color
  201.     call    display_string
  202.     mov    cl,07                ;max length of input string
  203.     mov    dx,0322h            ;display location
  204.     mov    di,offset payment_buf
  205.     call    qget_string
  206.     mov    ax,2                ;token #
  207.     call    text_in                ;es:di point at text, ax=token
  208. ;
  209. ; get interest rate
  210. ;
  211.     mov    si,offset interest_msg
  212.     mov    dx,0401h            ;display location
  213.     mov    ah,color
  214.     call    display_string
  215.     mov    cl,05                ;max length of input string
  216.     mov    dx,0422h            ;display location
  217.     mov    di,offset interest_buf
  218.     call    qget_string
  219.     mov    ax,0                ;token #
  220.     call    text_in                ;es:di point at text, ax=token
  221. ;
  222. ; zero all remaining variables
  223. ;
  224.     mov    ax,0
  225.     mov    dx,0                ;set value of zero
  226.     mov    bx,3                ;start with token 3
  227.     call    binary_in
  228.     mov    bx,4
  229.     call    binary_in
  230.     mov    bx,5
  231.     call    binary_in
  232.     mov    bx,6
  233.     call    binary_in
  234.     mov    bx,7
  235.     call    binary_in
  236. ;
  237. ; display header line
  238. ;
  239.     mov    si,offset header_msg
  240.     mov    dx,0601h            ;display location
  241.     mov    ah,color
  242.     call    display_string
  243.     
  244. ; compute int = int/1200
  245.  
  246.     mov    bx,5
  247.     mov    ax,1200
  248.     mov    dx,0
  249.     call    binary_in            ;token 5 = 1200
  250.  
  251.     mov    ax,0
  252.     mov    bx,5
  253.     mov    cx,0
  254.     call    f_divide                ;int/1200 -> int
  255.     
  256. ; compute int paid = int * prin
  257.  
  258. month_loop:
  259.     mov    ax,0                ;interest
  260.     mov    bx,1                ;prin
  261.     mov    cx,3                ;int paid
  262.     call    f_multiply
  263.  
  264.     mov    ax,3                ;int paid
  265.     call    round
  266.  
  267. ; compute prin paid = payment - int paid
  268.  
  269.     mov    ax,2                ;payment
  270.     mov    bx,3                ;int paid
  271.     mov    cx,4                ;prin paid
  272.     call    f_subtract
  273.  
  274.     mov    ax,4
  275.     call    round
  276.  
  277. ; compute x = prin - prin paid
  278.  
  279.     mov    ax,1                ;prin
  280.     mov    bx,4                ;prin paid
  281.     mov    cx,5                ;temp (x) prin remain
  282.     call    f_subtract
  283.     
  284. ; if x is positive
  285. ;   prin = x
  286.     mov    bx,5
  287.     call    binary_out
  288.     test    dh,80h                ;check if negative
  289.     jz    loan_cont            ; continue if numbers valid
  290.     jmp    view_wait            ; jmp if negative
  291. loan_cont:    
  292.     mov    ax,5
  293.     mov    bx,1
  294.     call    fmove    
  295.     
  296. ;   compute total int = total int + int paid
  297.  
  298.     mov    ax,6                ;total int paid
  299.     mov    bx,3                ;int paid
  300.     mov    cx,6                ;total int paid
  301.     call    f_addition
  302.  
  303. ;   total prin= total prin + prin paid
  304.  
  305.     mov    ax,7                ;total prin paid
  306.     mov    bx,4                ;prin paid
  307.     mov    cx,7
  308.     call    f_addition
  309. ;
  310. ; convert floating values to text
  311. ;
  312.     mov    ax,3                ;int paid
  313.     mov    di,offset int_paid_text
  314.     mov    cx,0208h            ;buffer sizes
  315.     call    text_out
  316.  
  317.     mov    ax,4                ;prin paid
  318.     mov    di,offset prin_paid_text
  319.     mov    cx,0209h            ;buffer sizes
  320.     call    text_out
  321.  
  322.     mov    ax,1                ;prin remaining
  323.     mov    di,offset prin_remain_text
  324.     mov    cx,020bh            ;buffer sizes
  325.     call    text_out
  326.     
  327. ; print results for this month
  328.  
  329.     mov    dh,display_row
  330.     mov    dl,1
  331.     mov    si,offset payment_buf
  332.     mov    ah,color
  333.     call    display_